package com.acme.chat.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import com.acme.chat.client.ChatClient;
import com.acme.chat.client.ClientListener;
import com.acme.chat.message.ChatMessage;
import com.acme.chat.message.Message;

public class TCPClient implements ChatClient, Runnable {
    private Socket socket;
    private String room;
    private ObjectInputStream sInput;
    private ObjectOutputStream sOutput;
    private String name;
    private ChatMessage chatMessage;

    private volatile boolean keepGoing = true;

    private Server server;

    public TCPClient(Socket socket, Server server) {
        // id = ++uniqueId;
        this.socket = socket;
        this.server = server;

        // System.out.println("Thread trying to create Object Input/Output Streams");
        try {
            sOutput = new ObjectOutputStream(socket.getOutputStream());
            sInput = new ObjectInputStream(socket.getInputStream());

            // read userName and room

            name = (String) sInput.readObject();
            room = (String) sInput.readObject();

            // Check username and room - duplicate, 100 symbols
            // display(username + " just connected" + " in " + room + " room.");
        } catch (IOException e) {
            // display("Exception creating new Input/output Streams: " + e);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        server.addClient(this);
    }

    public String getRoom() {
        return room;
    }

    public String getName() {
        return name;
    }

    public boolean sendMessage(Message message) {
        try {
            sOutput.writeObject(message);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void start() {
        (new Thread(this)).start();
    }

    public void run() {
        while (keepGoing) {
            try {
                chatMessage = (ChatMessage) sInput.readObject();
                processMessage(chatMessage);
            } catch (IOException e) {
                // display(username + " Exception reading Streams: " + e);
                e.printStackTrace();
                server.removeClient(this);
                keepGoing = false;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                server.removeClient(this);
                keepGoing = false;
            }
        }

        close();
    }

    private void processMessage(Message message) {
        server.processMessage(message, this);
    }

    private void close() {
        try {
            if (sOutput != null)
                sOutput.close();
        } catch (Exception e) {
        }

        try {
            if (sInput != null)
                sInput.close();
        } catch (Exception e) {
        }
        try {
            if (socket != null)
                socket.close();
        } catch (Exception e) {
        }
    }

    public void disconnect() {
        keepGoing = false;
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ChatClient)) {
            return false;
        }
        return this.name.equals(((ChatClient) o).getName());
    }

    @Override
    public String toString() {
        return "TCPClient: [name=" + name + "] [room=" + room + "] [socket=" + socket + "]";
    }

    public void connect(Server server) {
        // TODO Auto-generated method stub

    }

    public boolean isConnected() {
        // TODO Auto-generated method stub
        return false;
    }

    public void incomingMessage(Message message) {
        // TODO Auto-generated method stub

    }

    public void addListener(ClientListener listener) {
        // TODO Auto-generated method stub

    }
}
